package com.example.demo.service;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.file.Path;
import java.nio.file.Paths;

@Configuration
public class ResourceConfig implements WebMvcConfigurer {

    // 获取项目运行时资源目录路径
    private final String RESOURCE_DIR = Paths.get(
            System.getProperty("user.dir"),
            "src/main/resources/static/images"
    ).toAbsolutePath().toString();

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 映射物理路径到URL
        registry.addResourceHandler("/images/**")
                .addResourceLocations("file:" + RESOURCE_DIR + "/")
                .setCachePeriod(3600);
    }
}